﻿#ifndef PLCCLIENTMANAGE_H
#define PLCCLIENTMANAGE_H

#include <QObject>
#include <QThread>
#include "PLCClientBase.h"
#include "PLCClientStatusWidget.h"
#include "WidgetPLCClient.h"

class PLCClientManage : public QObject
{
    Q_OBJECT
public:
    explicit PLCClientManage(QObject *parent = nullptr);
    QWidget* getPLCClientStatusWidget(){return wgtPLCClientStatus;}
    QWidget* getPLCClientManageWidget(){return mPLCClientManageWidget;}
    QMap<QString, PLCClientBase*> getPLCClientAll(){return mMapPLCClient;}
    //
    void setConfigWriteOneFunc(void (*func)(const PLCClientConfig& config));
    void setConfigReadAllFunc(QMap<QString, PLCClientConfig> (*func)());
    void setConfigReadOneFunc(PLCClientConfig (*func)(QString camName));
    void startWorker();
    void checkPLCClientStatus();
    void initPLCClientAll();
    void initWgtPLCClientStatus();
    void initPLCClientManageWidget();

public slots:
    void onAddOne(const PLCClientConfig& config);
    void onDeleteOne(const QString& name);
    void onConfigChanged(const PLCClientConfig& config);
signals:
    void sigPLCClientStatus(bool isOnLine);

private:
    void initPLCClientOne(PLCClientConfig config);

private:
    //函数指针，用于配置读写配置的函数
    void (*configWriteOne)(const PLCClientConfig& config);
    QMap<QString, PLCClientConfig> (*configReadAll)();
    PLCClientConfig (*configReadOne)(QString comName);

private:
    QMap<QString, PLCClientBase*> mMapPLCClient;
    QMap<QString, QThread*> mMapComThread;
    QMap<QString, bool> mMapComStart;
    PLCClientStatusWidget* wgtPLCClientStatus;
    WidgetPLCClient* mPLCClientManageWidget;
};

#endif // PLCCLIENTMANAGE_H
